/**
 * 
 */
package tk.sunlatus.project.usermanage.web.impl;

import java.util.List;

import org.bouncycastle.util.encoders.Hex;

import tk.sunlatus.project.common.constants.SunlatusConstants;
import tk.sunlatus.project.common.domain.Page;
import tk.sunlatus.project.security.SunlotusSecurityManager;
import tk.sunlatus.project.security.key.KeyManager;
import tk.sunlatus.project.usermanage.domain.User;
import tk.sunlatus.project.usermanage.service.UserService;
import tk.sunlatus.project.usermanage.web.Service4WebUI;

/**
 * 为web提供用户管理服务接口
 * 
 * @author sunlotus
 * 
 */
public class Service4WebUIImpl implements Service4WebUI {

    private UserService userService = null;

    private SunlotusSecurityManager sercurityManager = null;

    public User querUserByID(String userID) {
        return userService.queryByID(userID);
    }

    @Override
    public void createUser(User user) {
        user.setPassword(sercurityManager.digest(user.getPassword(), user.getUserID()));
        user.setInfoPwd(sercurityManager.digest(user.getInfoPwd(), user.getUserID()));
        user.setTradPwd(sercurityManager.digest(user.getTradPwd(), user.getUserID()));
        user.setSalt(new String(Hex.encode(user.getUserID().getBytes(SunlatusConstants.EncodeConstant.CHARSET)),
            SunlatusConstants.EncodeConstant.CHARSET));
        user.setVerifyInfo(KeyManager.mixKeyAndSalt(KeyManager.getKey(SunlatusConstants.EncodeConstant.KEY_IDEA,
            SunlatusConstants.EncodeConstant.KEY_IDEA_SIZE), user.getSalt()));

        userService.createUser(user);
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public UserService getUserService() {
        return userService;
    }

    public SunlotusSecurityManager getSercurityManager() {
        return sercurityManager;
    }

    public void setSercurityManager(SunlotusSecurityManager sercurityManager) {
        this.sercurityManager = sercurityManager;
    }

    @Override
    public List<User> querUserList(User user, Page page) {
        return userService.queryUserList(user, page);
    }

    @Override
    public User updateUser(User user) {
        // TODO Auto-generated method stub
        return userService.updateUser(user);
    }

    /**
     * 删除 {@inheritDoc}
     */
    @Override
    public void deleteUser(User user, boolean deregisterFlag) {
        userService.deleteUser(user, deregisterFlag);
    }

}
